package com.phpxiu.app.okhttp.cookies;

import android.content.Context;

import java.util.List;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

/**
 * @file
 * @brief 自定义封装okhttp cookies管理对象
 * @author 吴金洲
 * @note 自定义封装okhttp cookies管理对象，使http请求动维护cookies
 *
 */
public class OKHttpCookieManager implements CookieJar {
    private OKHttpCookieStore cookieStore;
    public OKHttpCookieManager(Context context) {
        cookieStore = new OKHttpCookieStore(context);
    }

    @Override
    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
        if (cookies != null && cookies.size() > 0) {
            for (Cookie item : cookies) {
                cookieStore.addCookie(url, item);
            }
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl url) {
        List<Cookie> cookies = cookieStore.get(url);
        return cookies;
    }
}
